package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ListView;

import com.smartthinking.CICPNL.R;

public class Cupon_Detalle extends Activity {

	public String POI_IDR;
	public ArrayList<Dictionary<String, String>> TraeDatos = null;
	public boolean flag = false;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_cupon_detalle);
		
		Bundle extras = getIntent().getExtras();
		if (extras!=null){
			POI_IDR = extras.getString("poi_idr");
			flag = extras.getBoolean("flag");
			Log.i("POI_IDR", ""+POI_IDR);
			new AsyncContenido().execute();
		}
	}
	
	public class AsyncContenido extends AsyncTask<Void, Void, Void>{

		@Override
		protected Void doInBackground(Void... params) {
			String strFavoritos = "";
			String query = null;
		    if (flag) {
		    	Utilities util = new Utilities();
		        strFavoritos =util.getCupones(Cupon_Detalle.this,Utilities.getUDID(getApplicationContext()));
		        if (strFavoritos.length()>0) {
		            strFavoritos = "and cup_idr in("+strFavoritos+")";
		        }else{
		            strFavoritos = "and cup_idr in(9999999)";
		        }
				query = "select a.*, c.poi_nombre, c.poi_direccion, c.poi_telefono, c.poi_horarios, " +
						"c.poi_lat, c.poi_lng, c.poi_www, c.poi_mail, c.poi_facebook, c.poi_twitter, " +
						"c.poi_desc, c.poi_desc_en, c.poi_thumb, (select count(*) from cupones_emitidos " +
						"b where a.cup_idr = b.cup_idr and a.eve_idr = b.eve_idr and b.udid = '"+Utilities.getUDID(getApplicationContext())+"') " +
						"as total from cupones a inner join pois c on a.poi_idr = c.poi_idr where a.eve_idr = "+Utilities.EVE_IDR+
						" "+strFavoritos;
		        
		    }else{
				query = "select a.*, c.poi_nombre, c.poi_direccion, c.poi_telefono, c.poi_horarios, " +
						"c.poi_lat, c.poi_lng, c.poi_www, c.poi_mail, c.poi_facebook, c.poi_twitter, " +
						"c.poi_desc, c.poi_desc_en, c.poi_thumb, (select count(*) from cupones_emitidos " +
						"b where a.cup_idr = b.cup_idr and a.eve_idr = b.eve_idr and b.udid = '"+Utilities.getUDID(getApplicationContext())+"') " +
						"as total from cupones a inner join pois c on a.poi_idr = c.poi_idr where a.eve_idr = "+Utilities.EVE_IDR+
						" and a.poi_idr = "+POI_IDR;
		    }

			TraeDatos = Utilities.qry2(getApplicationContext(), query);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			ListView lista = (ListView) findViewById(R.id.lista_cupones);
			CuponesDetalleArrayAdapter adapter = new CuponesDetalleArrayAdapter(Cupon_Detalle.this, TraeDatos);
			adapter.notifyDataSetChanged();
			lista.setAdapter(adapter);
		}
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ui_cupon_detalle, menu);
		return true;
	}

	public void voidFinish(View v){
		finish();
	}
	
	public void voidDescarga(View v){
		finish();
	}
	
	
	public void makeFavorito(View v){

	}
	
	public void makeUbicacion(View v){
		
	}
	
	
}
